//
// Created by tang on 23-3-12.
//

#ifndef PIINFO_DYNAMIC_LIBRARY_HPP
#define PIINFO_DYNAMIC_LIBRARY_HPP

#include <type_traits>

#if defined(_WIN32)
#include <Windows.h>
using dll_handle_t = HMODULE;
constexpr const dll_handle_t  invalid_dll = nullptr;
#else
using dll_handle_t = void*;
constexpr const dll_handle_t  invalid_dll = nullptr;
#endif

class DynamicLibrary final {
public:
    explicit DynamicLibrary(const std::string &dll_path);
    ~DynamicLibrary();

    inline explicit operator bool() const { return handle_ != invalid_dll; }

    template<typename FuncType>
    FuncType resolve(std::string_view func_name)
    {
        return reinterpret_cast<FuncType>(get_sym_addr (func_name, handle_));
    }

private:
    static void* get_sym_addr(std::string_view sym_name, dll_handle_t handle);

    dll_handle_t handle_ {invalid_dll};
};

#endif  // PIINFO_DYNAMIC_LIBRARY_HPP
